import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_article_card.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class PreServiceEducationIndexPage extends StatefulWidget {
  const PreServiceEducationIndexPage({Key? key}) : super(key: key);

  @override
  State<PreServiceEducationIndexPage> createState() =>
      _PreServiceEducationIndexPageState();
}

class _PreServiceEducationIndexPageState
    extends State<PreServiceEducationIndexPage> {
  final controller = Get.find<PreServiceEducationIndexController>();
  final state = Get.find<PreServiceEducationIndexController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Scaffold(
      backgroundColor: Colors.transparent,
      appBar: TDNavBar(
        backgroundColor: Colors.transparent,
        title: '役前教育',
      ),
      body: Obx(() => _buildBody()),
    ));
  }

  _buildModule() {
    var moduleList = [
      {
        "title": "政策要闻",
        "icon": "https://minio.sxmbxt.com/bdjw/zcyw.png",
        "onTap": () {
          Get.toNamed(RouterName.preServiceEducationPolicyNews,
              arguments: {"title": "政策要闻", "typeId": 301});
        }
      },
      {
        "title": "学习课堂",
        "icon": "https://minio.sxmbxt.com/bdjw/xxkt.png",
        "onTap": () {
          Get.toNamed(RouterName.preServiceEducationPolicyNews,
              arguments: {"title": "学习课堂", "typeId": 302});
        }
      },
      {
        "title": "在线考试",
        "icon": "https://minio.sxmbxt.com/bdjw/zxks.png",
        "onTap": () {
          Get.toNamed(RouterName.examinationList);
        }
      },
      {
        "title": "问卷调查",
        "icon": "https://minio.sxmbxt.com/bdjw/wjdc.png",
        "onTap": () {
          Get.toNamed(RouterName.questionnaireList);
        }
      }
    ];

    return Container(
      width: Get.width,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
      ),
      child: Wrap(
        alignment: WrapAlignment.start,
        children: [
          for (int i = 0; i < moduleList.length; i++)
            _buildModuleItem(moduleList[i]['icon'], moduleList[i]['title'],
                moduleList[i]['onTap']),
        ],
      ),
    );
  }

  _buildModuleItem(icon, name, onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
          width: (Get.width - AppTheme.iDefaultPadding * 2) / 4,
          padding: EdgeInsets.all(AppTheme.iDefaultPadding),
          child: Column(children: [
            MkLoadImage(
              icon,
              width: 40,
              height: 40,
            ),
            Gaps.vGap8,
            Text(
              name,
              style: TextStyle(fontSize: 12),
            )
          ])),
    );
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.only(
          left: AppTheme.iDefaultPadding, right: AppTheme.iDefaultPadding),
      child: Column(
        children: [
          state.carouselList.length == 0? SizedBox(): Container(
              height: 160,
              child: Swiper(
                itemCount: state.carouselList.length,
                pagination: const SwiperPagination(
                    alignment: Alignment.bottomCenter,
                    builder: TDSwiperPagination.dotsBar),
                itemBuilder: (BuildContext context, int index) {
                  return TDImage(
                    imgUrl: state.carouselList[index].imgUrl,
                  );
                },
              )),
          Gaps.vGap12,
          _buildModule(),
          Gaps.vGap12,
          Expanded(
            child: EasyRefresh(
              // 下拉样式
              header: TDRefreshHeader(),
              footer: TaurusFooter(),
              child: Column(
                children: state.articleList
                    .map((e) => AppArticleCard(
                          article: e,
                          onTap: () {
                            controller.toArticleDetail(e);
                          },
                        ))
                    .toList(),
              ),
              // 下拉刷新回调
              onRefresh: controller.onRefresh,
              onLoad: controller.onLoad,
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<PreServiceEducationIndexController>();
    super.dispose();
  }
}
